let roomAPI           = require('../../API/RoomAPI');
var RoomProto         = require('../../API/RoomProto');
var RoomMessageRouter = 'game.gameHandler.roomMessageNotify';
var NNModel           = require('./NNModel');
var enumeration       = require('../../Constant/enumeration');

cc.Class({
	extends: cc.Component,
	properties: {
	},

	onLoad: function () {
		this.itemNode = cc.find('Item', this.node);
		this.content = cc.find('ScrollView/view/content', this.node);
		let data = this.dialogParameters;
		this.init(data.result, data.winMost, data.loseMost, data.creater);
		if (!NNModel.isUnionCreate() || NNModel.getCurBureau() != NNModel.getMaxBureau() || NNModel.getMyChairID() >= NNModel.getChairCount()) {
			cc.find('ButtonContinue', this.node).active = false;
		}
		if (data.hongBaoList && data.hongBaoList[NNModel.getMyChairID()] >= 0) {
			Global.DialogManager.createDialog("Game/GameHongBaoDialog", {
				score: data.hongBaoList[NNModel.getMyChairID()],
				hongBaoList: data.hongBaoList,
			});
		}
	},

	init: function (data, winMost, loseMost, creater) {
		for (let item of data) {
			let node = cc.instantiate(this.itemNode);
			let name = cc.find('name', node);
			let id = cc.find('id', node);
			let head = cc.find('head', node);
			name.getComponent(cc.Label).string = item.nickname;
			id.getComponent(cc.Label).string = 'ID:'+item.uid;
			if (item.score > 0) {
				let label = cc.find('win', node);
				label.getComponent(cc.Label).string = '+'+parseInt(item.score);
				cc.find('lose', node).active = false;
			}
			else {
				let label = cc.find('lose', node);
				label.getComponent(cc.Label).string = parseInt(item.score);
				cc.find('win', node).active = false;
			}
			Global.CCHelper.updateSpriteFrame(item.avatar, head.getComponent(cc.Sprite));

			if (winMost == loseMost) {
				cc.find('winmost', node).active = false;
				cc.find('losemost', node).active = false;
			}
			else {
				cc.find('winmost', node).active = (item.uid == winMost);
				cc.find('losemost', node).active = (item.uid == loseMost);
			}
			node.parent = this.content;
			node.active = true;
		}

		if (creater) {
			let name = cc.find('Creater/name', this.node);
			let id = cc.find('Creater/id', this.node);
			let head = cc.find('Creater/head', this.node);
			name.getComponent(cc.Label).string = creater.nickname; 
			id.getComponent(cc.Label).string = creater.uid; 
			Global.CCHelper.updateSpriteFrame(creater.avatar, head.getComponent(cc.Sprite));
		}
		else {
			cc.find('Creater', this.node).active = false;
		}
	},

	onButtonClick: function (event, param) {
        Global.AudioManager.playCommonSoundClickButton();
		if (param == 'back') {
			if (NNModel.isUnionCreate()) {
				Global.DialogManager.destroyDialog('Game/Niuniu/NNResult'); 
				if (NNModel.getRoomDismissReason() != enumeration.gameRoomDismissReason.UNION_OWNER_DISMISS) {
					Global.NetworkManager.notify(RoomMessageRouter, RoomProto.userLeaveRoomNotify());
				} 
				else {
					this.dialogParameters.enterHall();
				}
				NNModel.onDestroy();
			}
			else {
				Global.DialogManager.destroyDialog('Game/Niuniu/NNResult'); 
				this.dialogParameters.enterHall();
				NNModel.onDestroy();
			}
		}
		else if(param == 'share') {
			let imgPath = Global.CCHelper.capScreen(this.node);
			Global.PlatformHelper.wxShareImg(imgPath, 0);
		}
		else if(param == 'continue') {
			Global.DialogManager.destroyDialog('Game/Niuniu/NNResult'); 
			roomAPI.roomMessageNotify(RoomProto.getRoomSceneInfoNotify()); 
		}
	},
});
